package com.umfb.mongodb.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.umfb.demo.entity.User;
import com.umfb.demo.service.IUserService;
import com.umfb.mongodb.utils.Pagination;
import com.umfb.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zcah on 18-5-28.
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    MongoTemplate mongoTemplate;

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Override
    public User saveOrUpdate(User user) {

        if(StringUtils.isNotBlank(user.getUserId())){
            String email = user.getEmail();
            String nickname = user.getNickname();
            String phone = user.getPhone();
            String idCode = user.getIdCode();
            String realName = user.getRealName();

            Update update = new Update();
            update.set("email",email);
            update.set("nickname",nickname);
            update.set("phone",phone);
            update.set("updateAt",new Date());
            update.set("idCode",idCode);
            update.set("realName",realName);
            Query query = new Query(Criteria.where("userId").is(user.getUserId().trim()));
            mongoTemplate.updateFirst(query,update,User.class);

        }else {
            user.setUserId(IdWorker.getIdWorker().nextId());
            user.setCreateAt(new Date());

            mongoTemplate.save(user);

        }

        return user;
    }


    @Override
    public User findUserByUsernameAndPassword(User user) {
        if(StringUtils.isBlank(user.getEmail().trim())){

        }
        logger.error("email:{}",user.getEmail().trim());
        Query query = new Query(Criteria.where("email").is(user.getEmail().trim()).and("password").is(user.getPassword().trim()));

        User result = mongoTemplate.findOne(query, User.class);
        return result;
    }

    @Override
    public User getUserByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            logger.error("用户id user_id:{}"+userId);
        }
        Query query = new Query();
        Criteria cr = new Criteria();
        cr.and("userId").is(userId);
        query.addCriteria(cr);
        User user = mongoTemplate.findOne(query, User.class);

        return user;
    }

    @Override
    public Map<String,Object> findAll(int pageNo, int pageSize, Map<String, Object> paraMap) {

        Criteria cr = new Criteria();
        String phone = (String) paraMap.get("phone");
        String email = (String) paraMap.get("email");
        if(StringUtils.isNotBlank(phone)){
            cr.and("phone").is(phone);
        }
        if(StringUtils.isNotBlank(email)){
            cr.and("email").is(email);
        }
        Query query = new Query(cr);
        long totalCount = this.mongoTemplate.count(query, User.class);
        Pagination page = new Pagination(pageNo, pageSize, totalCount);
        query.skip(page.getFirstResult());// skip相当于从那条记录开始
        query.limit(pageSize);// 从skip开始,取多少条记录
        List<User> users = mongoTemplate.find(query, User.class);
        long count = mongoTemplate.count(query, User.class);
        Map<String,Object> map = new HashMap<>();
        map.put("dataList",users);
        map.put("count",count);
        return map;
    }
}
